package com.lingxinst.business.main.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.lingxinst.business.main.R
import com.lingxinst.business.main.databinding.ActivityMainBinding
import com.lingxinst.business.main.vm.MainViewModel
import com.lingxinst.farm.base.ui.BaseMvvmActivity
import com.orhanobut.logger.Logger

class MainActivity : BaseMvvmActivity<ActivityMainBinding, MainViewModel>(R.layout.activity_main),
    NavigationBarView.OnItemSelectedListener {

    private lateinit var navController: NavController
    private lateinit var navView: BottomNavigationView

    private lateinit var launcher: ActivityResultLauncher<Intent>

    private val bottomMenuId = arrayOf(
        R.id.navigation_home,
        R.id.navigation_channel,
        R.id.navigation_dynamic,
        R.id.navigation_mine
    )


    override fun initTag(): String {
        return this.javaClass.simpleName
    }

    override fun getVMClass(): Class<MainViewModel> {
        return MainViewModel::class.java
    }


    override fun initUIView(savedInstanceState: Bundle?) {
        navView = mBinding.navView
        navView.itemIconTintList = null

        navController = findNavController(R.id.nav_host_fragment_activity_main)
        navView.setupWithNavController(navController)
        navView.setOnItemSelectedListener(this)
    }

    override fun setObserver() {
        super.setObserver()
        launcher =
            ComponentActivity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                when (result.resultCode) {
                    ResultCode.LOGIN_SUCCESS, ResultCode.LOGIN_BACK -> {
                        // 导航到当前菜单页面
                        navController.navigate(bottomMenuId[mViewModel.curMenuPos.value!!])
                    }
                }

            }
        mViewModel.curMenuPos.observe(this) {
            when (it) {
                2, 3 -> {
                    if (checkLogin()) {
                        navController.navigate(bottomMenuId[it])
                    } else {
//                        PageUtils.jumpLogin(this)
                        launcher.launch(Intent(this, LoginActivity::class.java))
                    }
                }
                else ->{
                    navController.navigate(bottomMenuId[it])
                }
            }
        }

    }

    /**
     * singleTask 启动模式：
     * 未销毁重新跳转到该页面时在该方法中接收参数
     */
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        Logger.e("onNewIntent：${tag}")
    }

    /**
     * Toolbar 菜单栏控件选择操作
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // 根据菜单栏控件的id进行选择操作
        when (item.itemId) {
            // 如果是返回按钮
            android.R.id.home -> {
                // 结束当前活动
                Activity.finish()
                // 返回true表示已经处理了该事件
                return true
            }

            // 其他情况
            else -> {
                // 调用父类的onOptionsItemSelected方法
                return super.onOptionsItemSelected(item)
            }
        }


    }

    /**
     * 切换底部菜单
     */
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        return mViewModel.onNavigationItemSelected(item)
    }


}